dojo.provide("miaigo.model.sgf.SGFNode");

dojo.require("miaigo.model.Node");

dojo.declare(
    "miaigo.model.sgf.SGFNode",
    [miaigo.model.Node],
    {
	// Root nodes are the first nodes of gametrees,
	// which are direct descendants from a collection
	// some properties can only in root node
	_isRoot: false,

	// Move nodes have move properties:
	// (B, KO, MN, W, BL, OB, OW, WL, BM, DO, IT, TE)
	_isMove: false,

	// Setup nodes have setup properties:
	// (AB, AE, AW, PL)
	// Move properties must not be mixed with setup
	// properties within the same node.
	_isSetup: false,

	// Info nodes have info properties
	_isInfo: false,

	// An array of SGFTree as variations of next node
	// for a game, _variations[0] contains the next move node.
	_variations: null,

	// SGFTree.
	// Which tree this node belong to.
	_parentTree: null,

	// Array of properties (SGFProperty)
	_properties: null,

	constructor: function(createParam)
	{
	    this._properties = [];
	    this._variations = [];

	    if (createParam)
	    {
		this._isRoot = createParam.isRoot ? true : false;
		this._parentTree = createParam.parent;
	    }
	},

	isRoot: function() { return this._isRoot; },
	isMove: function() { return this._isMove; },
	isSetup: function() { return this._isSetup; },
	isInfo: function() { return this._isInfo; },

	getParent: function() { return this._parentTree; },

	getVariationCount: function() { return this._variations.length; },
	getVariation: function(index) { return this._variations[index]; },

	addVariation: function(/* miaigo.model.sgf.SGFTree */tree)
	{
	    this._variations.push(tree);
	},

	// Test if property to be added is legal.
	// e.g. move and setup properties cannot in the same node
	_validateProperty: function(/* SGFProperty */prop)
	{
	    if (prop)
	    {
		var propType = prop.getType();

		if (this.isMove() && propType == "setup")
		{
		    // setup property cannot in move node
		    return false;
		}
		if (this.isSetup() && propType == "move")
		{
		    // move property cannot in setup node
		    return false;
		}
		if (propType == "root")
		{
		    if (!this.isRoot())
		    {
			// root property can in root node only
			return false;
		    }
		}

		return true;
	    }
	    else
	    {
		return false;
	    }
	},

	// add a property to this node
	addProperty: function(prop)
	{
	    if (this._validateProperty(prop))
	    {
		var propType = prop.getType();

		this._properties.push(prop);

		// update node type
		if (propType == "move")
		{
		    this._isMove = true;
		}
		
		if (propType == "setup")
		{
		    this._isSetup = true;
		}

		if (propType == "info")
		{
		    this._isInfo = true;
		}

		// fill in this._attrMap
		var attrName = miaigo.model.sgf.PropertyFactory.attributeName(prop.getID());
		this._attrMap[attrName] = prop.getValue();
	    }
	},

	// get properties count of this node
	getPropertyCount: function()
	{
	    return this._properties.length;
	},

	// get a property (a SGFProperty)by index
	getProperty: function(index)
	{
	    return this._properties[index];
	}
    });